<%def name="inline_edit(inline_admin_formset, formset_count)">
<%namespace name="admin" module="django.contrib.admin.templatetags.adminmedia" />
<div class="inline-group" id="${ inline_admin_formset.formset.prefix }-group">
  <div class="tabular inline-related${' last-related' if formset_count==0 else ''}">
<%!
    # filter to titlecase text
    def title(text):
        return text.title()
%>

${inline_admin_formset.formset.management_form}
<fieldset class="module">
   <h2>${ inline_admin_formset.opts.verbose_name_plural|title,h }</h2>
   ${inline_admin_formset.formset.non_form_errors()}
   <table>
     <thead><tr>

% for i, field in enumerate(inline_admin_formset.fields()):
       % if not field.widget.is_hidden:
         <th ${ 'colspan="2"' if i==0 else '' }${' class="required"' if field.required else '' }>${ capfirst(field.label) if field.label else "" }</th>
       % endif
% endfor
     
% if inline_admin_formset.formset.can_delete:
<th>${trans("Delete?")}</th>
% endif
</tr></thead>

<tbody>
<%
    inline_admin_formset_list = [X for X in inline_admin_formset]
%>
% for row,inline_admin_form in enumerate(inline_admin_formset_list):
  % if inline_admin_form.form.non_field_errors():
    <tr><td colspan="${ inline_admin_form.field_count() }">${ inline_admin_form.form.non_field_errors() }</td></tr>
  % endif
    <tr class="${ "row1" if row % 2 == 0 else "row2" } ${'has_original' if inline_admin_form.original or inline_admin_form.show_url else ''} ${'empty-form' if row==len(inline_admin_formset_list)-1 else ''}"
        id="${ inline_admin_formset.formset.prefix }-${ row if row!=len(inline_admin_formset_list)-1 else 'empty'}">
<td class="original">
% if inline_admin_form.show_url:
<p>
<a href="../../../r/${inline_admin_form.original.content_type_id}/${inline_admin_form.original.id}/">${trans("View on site")}</a>
</p>
% endif
${inline_admin_form.pk_field().field}
% for fieldset in inline_admin_form:
    % for line in fieldset:
        % for field in line:
            % if hasattr(field.field, "is_hidden") and field.field.is_hidden:
            ${field.field} 
            % endif
        % endfor
    % endfor
% endfor
</td>
     
% for fieldset in inline_admin_form:
% for line in fieldset:
% for field in line:
<td class="${field.field.name}">
              % if hasattr(field,'is_readonly') and field.is_readonly:
                  <p>${ field.contents }</p>
              % else:
                  ${ field.field.errors.as_ul() }
                  ${ field.field }
              % endif
</td> 
% endfor
% endfor
% endfor

% if inline_admin_formset.formset.can_delete:
<td class="delete">
% if inline_admin_form.original:
${inline_admin_form.deletion_field().field.as_widget()}
% endif
</td> 
% endif
        </tr>
% endfor
     </tbody>
   </table>   
</fieldset>
  </div>
</div>

<script type="text/javascript">
(function($) {
    $(document).ready(function($) {
        var rows = "#${ inline_admin_formset.formset.prefix }-group .tabular.inline-related tbody tr";
        var alternatingRows = function(row) {
            $(rows).not(".add-row").removeClass("row1 row2")
                .filter(":even").addClass("row1").end()
                .filter(rows + ":odd").addClass("row2");
        }
        var reinitDateTimeShortCuts = function() {
            // Reinitialize the calendar and clock widgets by force
            if (typeof DateTimeShortcuts != "undefined") {
                $(".datetimeshortcuts").remove();
                DateTimeShortcuts.init();
            }
        }
        var updateSelectFilter = function() {
            // If any SelectFilter widgets are a part of the new form,
            // instantiate a new SelectFilter instance for it.
            if (typeof SelectFilter != "undefined"){
                $(".selectfilter").each(function(index, value){
                  var namearr = value.name.split('-');
                  SelectFilter.init(value.id, namearr[namearr.length-1], false, "${ admin.admin_media_prefix() }");
                })
                $(".selectfilterstacked").each(function(index, value){
                  var namearr = value.name.split('-');
                  SelectFilter.init(value.id, namearr[namearr.length-1], true, "${ admin.admin_media_prefix() }");
                })
            }
        }
        var initPrepopulatedFields = function(row) {
            return;
            row.find('.prepopulated_field').each(function() {
                var field = $(this);
                var input = field.find('input, select, textarea');
                var dependency_list = input.data('dependency_list') || [];
                var dependencies = row.find(dependency_list.join(',')).find('input, select, textarea');
                if (dependencies.length) {
                    input.prepopulate(dependencies, input.attr('maxlength'));
                }
            });
        }
        $(rows).formset({
            prefix: "${ inline_admin_formset.formset.prefix }",
            addText: "Add another ${ title(inline_admin_formset.opts.verbose_name) }",
            numCols: "${inline_admin_form.field_count()}",
            formCssClass: "dynamic-${ inline_admin_formset.formset.prefix }",
            deleteCssClass: "inline-deletelink",
            deleteText: "${ trans('Remove') }",
            emptyCssClass: "empty-form",
            removed: alternatingRows,
            added: (function(row) {
                initPrepopulatedFields(row);
                reinitDateTimeShortCuts();
                updateSelectFilter();
                alternatingRows(row);
            })
        });
    });
})(django.jQuery);
</script>

</%def>
